//////////////////////////////////////////////////////////////////////////////
//                                                                          //
//  Filename: DebugMemoryLeaks.h                                            //
//  Create date: July 08, 2005                                              //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////
//                                                                          //
//  Description: This file leverages the Microsoft C Runtime Library's      //
//               ability to report memory leaks in DEBUG builds,  This      //
//               capability is already present in MFC applications          //
//               (see DEBUG_NEW).                                           //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////
//       Copyright (c) 2005 - Realtime Gaming.  All rights reserved.        //
//////////////////////////////////////////////////////////////////////////////

#ifndef __DEBUGMEMORYLEAKS__
#define __DEBUGMEMORYLEAKS__

//////////////////////////////////////////////////////////////////////////////
// Provide a version of DEBUG_NEW that does not require MFC and make sure   //
// that DEBUG build always uses DEBUG_NEW.                                  //
//                                                                          //
//  The DEBUG_NEW macro will report the file and line number where the      //
//  leaked memory was allocated as long as this header file was included    //
//  at the point of allocation.                                             //
//                                                                          //
//	See also Microsoft Knowledge Base article Q140858						//
//	PRB: _CRTDBG_MAP_ALLOC Does Not Work as Documented						//
//////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
#undef new
#ifndef DEBUG_NEW
#ifndef _MFC_OVERRIDES_NEW
#ifndef PM_GLOBAL_PROTECTION
#define _CRTDBG_MAP_ALLOC

#ifndef _INC_STDLIB
#include <stdlib.h>
#endif //_INC_STDLIB

#ifndef _INC_MALLOC
#include <malloc.h>
#endif //_INC_MALLOC

#ifndef _INC_NEW
#include <new.h>
#endif //_INC_NEW

#ifndef _NEW_
#include <new>
#endif //_NEW_

#ifndef _MEMORY_
#include <memory>
#endif //_MEMORY_

#ifndef _INC_STRING
#include <string.h>
#endif //_INC_STRING

#undef _CRTDBG_MAP_ALLOC

#ifndef _INC_CRTDBG
#include <crtdbg.h>
#endif //_INC_CRTDBG

#define _CRTDBG_MAP_ALLOC
#define   malloc(s)         _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define   calloc(c, s)      _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define   realloc(p, s)     _realloc_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define   _expand(p, s)     _expand_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
#define   free(p)           _free_dbg(p, _NORMAL_BLOCK)
#define   _msize(p)         _msize_dbg(p, _NORMAL_BLOCK)
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__)
#endif //_MFC_OVERRIDES_NEW
#endif //DEBUG_NEW
#define new DEBUG_NEW
#endif //PM_GLOBAL_PROTECTION
#endif //_DEBUG

#endif